﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;

using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Text;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Common.Serialization;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.DL.Data.Local.Services
{
    /// <summary>
    /// This class is used to load all data below a given project.
    /// </summary>
    internal class InitialDataCacheLoader : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string ProjectLoadPart = "Project";

        private readonly DataContext _context;
        private readonly IDataImportMonitor _importMonitor;
        private readonly ValueChain<string> _projectIdChain;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new InitialDataCacheLoader instance, no public constructor
        /// needed.
        /// </summary>
        internal InitialDataCacheLoader(DataContext context, ValueChain<string> projectIdChain, IDataImportMonitor importMonitor)
        {
            PreCondition.AssertNotNull(importMonitor, "importMonitor");
            PreCondition.AssertNotNull(projectIdChain, "projectIdChain");
            PreCondition.AssertNotNull(context, "context");

            _context = context;
            _projectIdChain = projectIdChain;
            _importMonitor = importMonitor;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        internal void LoadData()
        {
            Exception error = null;
            _importMonitor.StartSubProgress(ProjectLoadPart);

            try
            {
                _context.DataProvider.LoadProjectData(_projectIdChain);
            }
            catch (Exception e)
            {
                error = e;
            }
            finally
            {
                _importMonitor.EndSubProgress(ProjectLoadPart, error);
            }
        }

        #endregion
    }
}